<%@ page import="java.util.*" %>
<%@ page import="com.dzsw.struts.bean.Option" %>
<%@ page import="org.apache.struts.util.LabelValueBean" %>
<%--
  Created by IntelliJ IDEA.
  User: SyDevuser24
  Date: 2024/11/15
  Time: 11:28
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="/WEB-INF/tld/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/tld/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/tld/struts-logic.tld" prefix="logic" %>

<%
    String contextPath = request.getContextPath();
    List<String> list = new ArrayList<>();
    list.add("str1");
    list.add("str2");
    list.add("str3");
    list.add("str4");
    list.add("str5");
    list.add("str6");
    list.add("str7");
    list.add("str8");
    Integer[] ints = new Integer[]{ 1, 2, 3, 4, 5 };
    Map<String, String> map = new LinkedHashMap<>();
    map.put(list.get(0), list.get(0) + "Value");
    map.put(list.get(1), list.get(1) + "Value");
    map.put(list.get(2), list.get(2) + "Value");
    map.put(list.get(3), list.get(3) + "Value");
    map.put(list.get(4), list.get(4) + "Value");
    map.put(list.get(5), list.get(5) + "Value");
    Map<String, Object> objMap = new HashMap<>();
    objMap.put("list", list);
    objMap.put("ints", ints);
    objMap.put("map", map);


    List<Option> optionList = new ArrayList<>();
    optionList.add(new Option("1", "沈阳"));
    optionList.add(new Option("2", "铁岭"));
    optionList.add(new Option("3", "抚顺"));
    optionList.add(new Option("4", "大连"));
    optionList.add(new Option("5", "葫芦岛"));
    optionList.add(new Option("6", "西丰"));
    optionList.add(new Option("7", "通辽"));

    List<LabelValueBean> labelValueList = new ArrayList<>();
    labelValueList.add(new LabelValueBean("沈阳", "1"));
    labelValueList.add(new LabelValueBean("铁岭", "2"));
    labelValueList.add(new LabelValueBean("抚顺", "3"));
    labelValueList.add(new LabelValueBean("大连", "4"));
    labelValueList.add(new LabelValueBean("葫芦岛", "5"));
    labelValueList.add(new LabelValueBean("西丰", "6"));
    labelValueList.add(new LabelValueBean("通辽", "7"));

    // 把变量放在上下文中，否则标签找不到。
    pageContext.setAttribute("list", list);
    pageContext.setAttribute("ints", ints);
    pageContext.setAttribute("intsLen", ints.length);
    pageContext.setAttribute("map", map);
    pageContext.setAttribute("objMap", objMap);
    pageContext.setAttribute("optionList", optionList);
    pageContext.setAttribute("labelValueList", labelValueList);
    application.setAttribute("foo", new Object());
%>

<html>
<head>
    <title>Tag test</title>
</head>
<body>
    <h2>&lt;bean:define &#47;&gt;</h2>
    <!-- bean:define id value -->
    <div>
        <h3>bean:define id value</h3>
        <bean:define id="str" value="Welcome struts tag." scope="request" />
        TAG=<bean:write name="str" />
        <br />
        EL=${str}
    </div>
    <!-- bean:define name property -->
    <div>
        <h3>bean:define name property</h3>
        <%
            request.setAttribute("sessionBean",session);
        %>
        <bean:define id="contextBean" name="sessionBean" property="servletContext"/>
        TAG=<bean:write name="contextBean" property="servletContextName" />
        <br />
        EL=${contextBean.servletContextName}
        <br />
        JSP=<%=session.getServletContext().getServletContextName()%>
    </div>
    <h2>&lt;bean:size &#47;&gt;</h2>
    <div>
        <bean:size id="listLength"  name="list" />
        <bean:size id="arrayLength"  name="ints" />
        <bean:size id="mapLength"  name="map" />
        <bean:size id="mapListLength" name="objMap" property="list" />
        <bean:size id="coll" collection="${objMap.list}" />
        len(list)=${listLength}
        <br />
        len(ints)=${arrayLength}
        <br />
        len(map)=${mapLength}
        <br />
        len(objMap.list)=${mapListLength}
        <br />
        len(coll)=${coll}
    </div>
    <h2>&lt;bean:write &#47;&gt;</h2>
    <div>
        mapValue=<bean:write name="map" property="str1" />
    </div>
    <h2>&lt;bean:message &#47;&gt;</h2>
    <div>
        <h3>
            <bean:message key="i18n.title" />
        </h3>
        <h4>
            <bean:message key="i18n.content" />
        </h4>
        <a href="<%=contextPath%>/langAction.do?lang=zh" target="_self">ToChinese</a>
        <br />
        <a href="<%=contextPath%>/langAction.do?lang=en" target="_self">ToEnglish</a>
    </div>
    <h2>&lt;logic:present &#47;&gt;</h2>
    <div>
        <logic:present name="list" scope="page">
            <h3>list in page</h3>
        </logic:present>
        <logic:present name="list" scope="request">
            <h3>list in request</h3>
        </logic:present>
        <logic:present name="list" scope="session">
            <h3>list in session</h3>
        </logic:present>
        <logic:present name="foo" scope="application">
            <h3>foo in application</h3>
        </logic:present>
        <logic:notPresent name="list" scope="application">
            <h3>list don't in application</h3>
        </logic:notPresent>
    </div>
    <h2>&lt;logic:iterate &#47;&gt;</h2>
    <div>
        <h3>Array</h3>
        TAG:
        <% String intsLen = String.valueOf(ints.length - 1); %>
        <logic:iterate id="i" name="ints" scope="page" indexId="idx">
            <bean:write name="i"/>
            <%if (idx < ints.length - 1) {%>
                ,
            <%}%>
            <logic:lessThan name="idx" value="<%=intsLen%>">
                ok
            </logic:lessThan>
        </logic:iterate>
        <br />
        EL:
        <logic:iterate id="i" name="ints" scope="page" indexId="idx">
            ${i}
            <%if (idx < ints.length - 1) {%>
                ,
            <%}%>
        </logic:iterate>
        <h3>Collection</h3>
        TAG:
        <logic:iterate id="item" name="list" scope="page" indexId="idx">
            <bean:write name="item"/>
            <%if (idx < list.size() - 1) {%>
                ,
            <%}%>
        </logic:iterate>
        <br />
        EL:
        <logic:iterate id="item" name="list" scope="page" indexId="idx">
            ${item}
            <%if (idx < list.size() - 1) {%>
                ,
            <%}%>
        </logic:iterate>
        <h3>Map</h3>
        TAG:
        <logic:iterate id="item" name="map" scope="page" indexId="idx">
            <bean:write name="item" property="key" /> --- <bean:write name="item" property="value" />
            <%if (idx < map.size() - 1) {%>
                ,
            <%}%>
        </logic:iterate>
        <br />
        EL:
        <logic:iterate id="item" name="map" scope="page" indexId="idx">
            ${item.key} --- ${item.value}
            <%if (idx < map.size() - 1) {%>
                ,
            <%}%>
        </logic:iterate>
    </div>
    <h2>&lt;html:select &#47;&gt;</h2>
    <div>
        <%
            out.write("<h3>labelValueList.value</h3>");
        %>
        <logic:iterate id="item" name="labelValueList" indexId="idx">
            ${idx} --- ${item.value} --- ${item.label}<br />
        </logic:iterate>
        <br />
        <h3>html</h3>
        <select property="selected">
            <logic:iterate id="item" name="optionList">
                <option value="${item.id}">${item.name}</option>
            </logic:iterate>
        </select>
        <br />
        <br />
        <html:form action="/myAction.do">
            <h3>html:option</h3>
            <html:select property="area1" value="2">
                <logic:iterate id="item" name="optionList">
                    <html:option value="${item.id}">${item.name}</html:option>
                </logic:iterate>
            </html:select>
            <br />
            <br />
            <h3>html:options</h3>
            <html:select property="area2" value="3">
                <html:options collection="optionList" property="id" labelProperty="name" />
            </html:select>
            <br />
            <br />
            <h3>html:optionsCollection</h3>
            <html:select property="area3" value="5">
                <html:optionsCollection name="labelValueList" />
            </html:select>
            <br />
            <br />
        </html:form>
    </div>
    <h2>&lt;html:radio &#47;&gt;</h2>
    <div>
        <html:form action="/myAction.do">
            <logic:iterate id="item" name="optionList">
                <html:radio property="radioArea" value="${item.id}"></html:radio>
                <bean:write name="item" property="name" />
            </logic:iterate>
        </html:form>
    </div>
    <h2>&lt;html:checkbox &#47;&gt;</h2>
    <div>
        <html:form action="/myAction.do">
            <h3>html:checkbox</h3>
            <html:checkbox property="checkbox1" value="1" />ON
            &nbsp;
            <html:checkbox property="checkbox2" value="2" />OFF
            <br />
            <h3>html:multibox</h3>
            <logic:iterate id="item" name="optionList">
                <html:multibox property="multiBox">
                    ${item.id}
                </html:multibox>
                ${item.name}
            </logic:iterate>
        </html:form>
    </div>
</body>
</html>
